package com.sskj.api.controller.admin;

import com.sskj.api.domain.Department;
import com.sskj.api.service.IDepartmentService;
import com.sskj.common.dto.TreeNode;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "部门模块")
@RestController
@RequestMapping("/admin/department")
@Validated
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @Operation(summary = "新增部门")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Department department) {
        departmentService.add(department);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除部门")
    @PostMapping("delete")
    public ApiResponse<Object> deleteById(@RequestParam Long id) {
        departmentService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新部门")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Department department) {
        departmentService.update(department);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "部门树结构")
    @GetMapping("tree")
    public ApiResponse<List<TreeNode<Department>>> tree() {
        List<TreeNode<Department>> tree = departmentService.tree();

        return ApiResponseUtil.success(tree);
    }
}
